/*
 * UIMultiSpinner.java
 *
 * Created on 2-jun-2009, 14:09:21
 *
 * This file is part of SimpLie.
 *
 * SimpLie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimpLie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SimpLie.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package edu.simplie.ui.reusable;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * A convenience class for an array of spinners.
 *
 * @author Teake Nutma
 * @version $Revision: 428 $, $Date: 2009-06-04 14:03:31 +0000 (Thu, 04 Jun 2009) $
 */
public class UIMultiSpinner extends javax.swing.JPanel implements ChangeListener
{

	/* Number of spinners */
	private int num = 0;
	/* Minimum value the spinners can have */
	private Integer minValue = null;
	/* Maximum value the spinners can have */
	private Integer maxValue = null;
	/* The preferred size of the spinners */
	private Dimension size;

	private ArrayList<ChangeListener> listeners;

    /** Creates new form UIMultiSpinner */
    public UIMultiSpinner()
	{
        initComponents();
		size = dummy.getPreferredSize();
		size.width = (int) (1.5 * size.width);
		listeners = new ArrayList<ChangeListener>();
    }

	public void addChangeListener(ChangeListener listener)
	{
		listeners.add(listener);
	}

	public void removeChangeListener(ChangeListener listener)
	{
		listeners.remove(listener);
	}

	protected void fireStateChanged()
	{
		ChangeEvent e = new ChangeEvent(this);
		for(ChangeListener listener : listeners)
		{
			listener.stateChanged(e);
		}
	}

	/**
	 * Sets the number of spinners.
	 *
	 * @param num	The number of spinners.
	 */
	public void setNumSpinners(int num)
	{
		if(num < 0)	return;
		container.removeAll();

		this.num = num;
		for(int i = 0; i < num; i++)
		{
			UISpinner spinner = new UISpinner();
			spinner.setPreferredSize(size);
			spinner.addChangeListener(this);
			container.add(spinner, i);
		}
		if(minValue != null) setMinValue(minValue);
		if(maxValue != null) setMaxValue(maxValue);
		validate();
		repaint();
	}

	/**
	 * Sets the minimum value the spinners can have.
	 *
	 * @param value	The minimum value.
	 */
	public void setMinValue(int value)
	{
		minValue = value;
		for(int i = 0; i < num; i++)
		{
			get(i).setMinValue(value);
		}
	}

	/**
	 * Sets the maximum value the spinners can have.
	 *
	 * @param value	The maximum value.
	 */
	public void setMaxValue(int value)
	{
		maxValue = value;
		for(int i = 0; i < num; i++)
		{
			get(i).setMinValue(value);
		}
	}

	/**
	 * Gets the values of all the spinners.
	 *
	 * @return An n-dimensional array of integers, where n is the number of spinners.
	 */
	public int[] getValues()
	{
		int[] values = new int[num];
		for(int i = 0; i < num; i++)
		{
			values[i] = get(i).getValue();
		}
		return values;
	}

	/**
	 * Sets the values of all the spinners.
	 *
	 * @param values	An n-dimensional array of integers, where n is the number of spinners.
	 * @return			False if n does not match the number of spinners.
	 */
	public boolean setValues(int[] values)
	{
		if(values.length != num)
			return false;

		for(int i = 0; i < num; i++)
		{
			get(i).setValue(values[i]);
		}
		
		return true;
	}

	/**
	 * Sets one value for all the spinners.
	 *
	 * @param value The value to set.
	 */
	public void setValue(int value)
	{
		for(int i = 0; i < num; i++)
		{
			get(i).setValue(value);
		}
	}

	public void stateChanged(ChangeEvent e)
	{
		fireStateChanged();
	}

	private UISpinner get(int index)
	{
		return (UISpinner) container.getComponent(index);
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        container = new javax.swing.JPanel();
        dummy = new edu.simplie.ui.reusable.UISpinner();

        setName("Form"); // NOI18N

        container.setName("container"); // NOI18N
        container.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        dummy.setName("dummy"); // NOI18N
        container.add(dummy);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(container, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(container, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel container;
    private edu.simplie.ui.reusable.UISpinner dummy;
    // End of variables declaration//GEN-END:variables

}
